<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { vitest } from 'vitest';
import { type MountingOptions, shallowMount } from '@vue/test-utils';
import axios from 'axios';
import sinon from 'sinon';
import { type RouteLocation } from 'vue-router';
import { createTestingPinia } from '@pinia/testing';

import AccountService from '../account.service';
import LoginForm from './login-form.vue';
import { useStore } from '@/store';

type LoginFormComponentType = InstanceType<typeof LoginForm>;

let route: Partial<RouteLocation>;
const routerGoMock = vitest.fn();
vitest.mock('vue-router', () => ({
  useRoute: () => route,
  useRouter: () => ({ go: routerGoMock }),
}));

const axiosStub = {
  get: sinon.stub(axios, 'get'),
  post: sinon.stub(axios, 'post'),
};

describe('LoginForm Component', () => {
  let loginForm: LoginFormComponentType;

  beforeEach(() => {
    route = {};
    axiosStub.get.resolves({});
    axiosStub.post.reset();

    const pinia = createTestingPinia();
    const store = useStore();

    const globalOptions: MountingOptions<LoginFormComponentType>['global'] = {
      stubs: {
        'b-alert': true,
        'b-button': true,
        'b-form': true,
        'b-form-input': true,
        'b-form-group': true,
        'b-form-checkbox': true,
        'b-link': true,
      },
      plugins: [pinia],
      provide: {
        accountService: new AccountService(store),
      },
    };
    const wrapper = shallowMount(LoginForm, { global: globalOptions });

    loginForm = wrapper.vm;
  });

<%_ if (authenticationTypeJwt) { _%>
  it('should not store token if authentication is KO', async () => {
    // GIVEN
    loginForm.login = 'login';
    loginForm.password = 'pwd';
    loginForm.rememberMe = true;
    axiosStub.post.rejects();

    // WHEN
    loginForm.doLogin();
    await loginForm.$nextTick();

    // THEN
    expect(
      axiosStub.post.calledWith('api/authenticate', {
        username: 'login',
        password: 'pwd',
        rememberMe: true,
      }),
    ).toBeTruthy();
    await loginForm.$nextTick();
    expect(loginForm.authenticationError).toBeTruthy();
  });

  it('should store token if authentication is OK', async () => {
    // GIVEN
    loginForm.login = 'login';
    loginForm.password = 'pwd';
    loginForm.rememberMe = true;
    const jwtSecret = 'jwt-secret';
    axiosStub.post.resolves({ headers: { authorization: `Bearer ${jwtSecret}` } });

    // WHEN
    loginForm.doLogin();
    await loginForm.$nextTick();

    // THEN
    expect(
      axiosStub.post.calledWith('api/authenticate', {
        username: 'login',
        password: 'pwd',
        rememberMe: true,
      }),
    ).toBeTruthy();

    expect(loginForm.authenticationError).toBeFalsy();
    expect(localStorage.getItem('<%=jhiPrefixDashed %>-authenticationToken')).toEqual(jwtSecret);
  });

  it('should store token if authentication is OK in session', async () => {
    // GIVEN
    loginForm.login = 'login';
    loginForm.password = 'pwd';
    loginForm.rememberMe = false;
    const jwtSecret = 'jwt-secret';
    axiosStub.post.resolves({ headers: { authorization: `Bearer ${jwtSecret}` } });

    // WHEN
    loginForm.doLogin();
    await loginForm.$nextTick();

    // THEN
    expect(
      axiosStub.post.calledWith('api/authenticate', {
        username: 'login',
        password: 'pwd',
        rememberMe: false,
      }),
    ).toBeTruthy();

    expect(loginForm.authenticationError).toBeFalsy();
    expect(sessionStorage.getItem('<%=jhiPrefixDashed %>-authenticationToken')).toEqual(jwtSecret);
  });
<%_ } else { _%>
  it('should authentication be KO', async () => {
    // GIVEN
    loginForm.login = 'login';
    loginForm.password = 'pwd';
    loginForm.rememberMe = true;
    axiosStub.post.rejects();

    // WHEN
    loginForm.doLogin();
    await loginForm.$nextTick();

    // THEN
    expect(
      axiosStub.post.calledWith('api/authentication', 'username=login&password=pwd&remember-me=true&submit=Login', {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      }),
    ).toBeTruthy();
    await loginForm.$nextTick();
    expect(loginForm.authenticationError).toBeTruthy();
  });

  it('should authentication be OK', async () => {
    // GIVEN
    loginForm.login = 'login';
    loginForm.password = 'pwd';
    loginForm.rememberMe = true;
    axiosStub.post.resolves({});

    // WHEN
    loginForm.doLogin();
    await loginForm.$nextTick();

    // THEN
    expect(
      axiosStub.post.calledWith('api/authentication', 'username=login&password=pwd&remember-me=true&submit=Login', {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      }),
    ).toBeTruthy();

    expect(loginForm.authenticationError).toBeFalsy();
  });
<%_ } _%>

});
